#!/bin/sh

test `which javac | wc -c` -eq 0 && echo "VMManager is a java application, you need java (java and javac)!" && exit 2
test `which java | wc -c` -eq 0 && echo "VMManager is a java application, you need java (java and javac)!" && exit 2

# JDBC - MySQL driver connector for Java
apt-get -y install libmysql-java

# JDOM - Library for manipulating XML files
apt-get -y install libjdom1-java

# uuid Runtime - Used to generate random password
apt-get -y install uuid-runtime

# kill current instances

cpid=`ps aux | grep VMManager | grep -v grep | awk '{print $2}'`
if [ ! -z $cpid ]; then
	echo Killing VMManager running with pid $cpid 
	kill -9 $cpid
fi

# Set java Classpath
VMMCLASSPATH=/usr/share/java/mysql-connector-java.jar:/usr/share/java/jdom1.jar

# Package
# VMManager classes are contained in a package called vmmjava
# Source code should be placed in a directory with the same name and
# java VMMCLASSPATH set acccordingly

INSTALL_DIR=$1
test -z $INSTALL_DIR && INSTALL_DIR=/opt/

INSTALL_DIR=$INSTALL_DIR/vmmjava
INSTALL_DIR=`echo $INSTALL_DIR | sed 's/\/\//\//g'`

mkdir -p $INSTALL_DIR
VMMCLASSPATH=$VMMCLASSPATH:$INSTALL_DIR

# Export Classpath
echo 'export CLASSPATH=$CLASSPATH:'$VMMCLASSPATH > /etc/profile.d/vmmjava.sh
export CLASSPATH=$CLASSPATH:$VMMCLASSPATH

echo "Installing files in $INSTALL_DIR"
cp -r * $INSTALL_DIR

echo "Set vmmanager password for MySQL o enter for autogenerated password."
read NEWPASS

test `echo $NEWPASS | wc -c` -lt 4 && echo "Generating password..." &&  NEWPASS=`uuidgen`
sed 's/\$\$PASSWORD\$\$/'"$NEWPASS"'/g' VMManager.java > $INSTALL_DIR/VMManager.java

echo "Installing DB Schema..."
sed 's/\$\$PASSWORD\$\$/'"$NEWPASS"'/g' db-schema.sql > $INSTALL_DIR/db-schema.sql
echo "Please, type your MySQL root password..."
mysql --force -u root -p < $INSTALL_DIR/db-schema.sql

echo "Encrypting db-schema with gpg..."
echo "Please, set a password to protect this file:"
gpg -c $INSTALL_DIR/db-schema.sql
rm -v $INSTALL_DIR/db-schema.sql


echo "Comipiling sources."
javac $INSTALL_DIR/VMManager.java

echo "Deleting source files."
rm -fR $INSTALL_DIR/*.java

# java vmmjava.VMManager port

BINFILE=/usr/local/bin/vmmanager-server
LOGFILE=/var/log/vmmanager.log
rm -fR $LOGFILE

echo '#!/bin/sh'  > $BINFILE
echo 'vmmport=17216'  >> $BINFILE
echo 'test `whoami` != "root" && echo "Mmm... are you root?" && exit 2'  >> $BINFILE
echo 'LOGFILE='$LOGFILE >> $BINFILE
echo 'test `whoami` != "root" && echo "Mmm... are you root?" && exit 2'  >> $BINFILE
echo 'if [ -z $1 ]; then echo Using default port 17216.' >> $BINFILE
echo 'else' >> $BINFILE
echo 'test `echo $1 | sed 's/^[0-9]*//' | wc -c` -ne 1  && echo "$1 is not a number [ neither a port ]." && exit 10' >> $BINFILE
echo 'test $1 -gt 65535 -o $1 -lt 1024 && echo "$1 out of range [valid 1024 - 65535]." && exit 11' >> $BINFILE
echo 'vmmport=$1' >> $BINFILE
echo 'fi' >> $BINFILE
echo 'test ` ps aux | grep "java VMManager" | grep -v grep | wc -l` -ne 0 && echo "VMManager already running!" && exit 12' >> $BINFILE
echo 'echo "[`date +%A\ %Y.%m.%d\ %R:%S`] Starting VMManager-server: listening port $vmmport" | tee -a $LOGFILE' >> $BINFILE
echo 'export CLASSPATH=$CLASSPATH:'$VMMCLASSPATH >> $BINFILE
echo 'echo "VMManager logfile : $LOGFILE" | tee -a $LOGFILE'>> $BINFILE
echo 'cd '$INSTALL_DIR'; java VMManager $vmmport >> $LOGFILE &' >> $BINFILE
echo 'exit 0' >> $BINFILE

chmod +x $BINFILE

echo "Installed!"
echo "Execute VMManager typing: vmmanager-server [port] ($BINFILE)"
